home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / QuickDraw™ GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / Main.c < prev    next >
Encoding:
Text File  |  1995-04-10  |  8.3 KB  |  497 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Main.c
  3.  *
  4.  *    Robert Dierkes,  November 11, 1993
  5.  */
  6.  
  7.  
  8. #undef    MAC_HEADERS
  9.  
  10.  
  11. /*------------------*/
  12. /*    Include Files    */
  13. /*------------------*/
  14. #ifndef    MAC_HEADERS
  15.     #include <Memory.h>
  16.     #include <Fonts.h>
  17.     #include <Menus.h>
  18.     #include <OSEvents.h>
  19.     #include <Dialogs.h>
  20.     #include <ToolUtils.h>
  21.     #include <Desk.h>
  22.     #include <GestaltEqu.h>
  23.     #include <LoMem.h>
  24. #endif    MAC_HEADERS
  25.  
  26. #include "graphics toolbox.h"
  27. #include "graphics libraries.h"
  28. #include "graphics debugging.h"
  29. #include "graphics macintosh.h"
  30.  
  31. #include "ResourceIDs.h"
  32. #include "Window.h"
  33. #include "ShapeAction.h"
  34. #include "AutoMouse.h"
  35.  
  36. #include "Main.h"
  37.  
  38.  
  39. /*----------------------*/
  40. /*    Global Declarations    */
  41. /*----------------------*/
  42. gxGraphicsClient    gClient;
  43. Boolean                gQuitApp;
  44. Handle                ghMainMenu;
  45. Rect                gDragRect;
  46. WindowPtr            gWindow;
  47.  
  48.  
  49. /*------------------------------*/
  50. /*    External Declarations     */
  51. /*------------------------------*/
  52. extern    GlobalStructure    globals;
  53.  
  54.  
  55.     Boolean
  56. InitApp (void)
  57. {
  58.     long            response;
  59.     gxGraphicsError    grfxError;
  60.  
  61. #ifndef linkedIn
  62.     if (Gestalt (gestaltGraphicsVersion, &response)) {
  63.         DebugStr ("\pQuickDraw GX is not installed");
  64.         return false;
  65.     }
  66. #endif
  67.  
  68.     if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
  69.         DebugStr ("\pGXNewGraphicsClient failed");
  70.         return false;
  71.     }
  72.  
  73.     /* Initialize Skia */
  74.     GXEnterGraphics ();
  75.  
  76.     GXSetValidation (gxNoValidation);
  77.     SetGraphicsLibraryErrors ();
  78.     SetGraphicsLibraryNotices();
  79.     InitCommonColors ();
  80.  
  81.     MaxApplZone();
  82.     MoreMasters();
  83.     MoreMasters();
  84.  
  85.     InitGraf (&qd.thePort);
  86.     InitFonts ();
  87.     FlushEvents (everyEvent, 0);
  88.     InitWindows ();
  89.     InitMenus ();
  90.     InitDialogs (nil);
  91.     InitCursor ();
  92.  
  93.     return true;
  94.  
  95. }    /* InitApp */
  96.  
  97.  
  98.     void
  99. ExitApp (void)
  100. {
  101.     gxViewPort    parent;
  102.  
  103.     if (gWindow)
  104.     {
  105.         if (parent = GXGetWindowViewPort (gWindow))
  106.             GXDisposeViewPort (parent);
  107.         DisposeDialog (gWindow);
  108.     }
  109.  
  110.        DisposeCommonColors ();
  111.     GXExitGraphics ();
  112.     if (gClient)
  113.         GXDisposeGraphicsClient (gClient);
  114.  
  115. }    /* ExitApp */
  116.  
  117.  
  118.     void
  119. InitializeMenus (void)
  120. {
  121.     ghMainMenu = GetNewMBar (menuBarRsrcID);
  122.     SetMenuBar (ghMainMenu);
  123.     AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
  124.     DrawMenuBar ();
  125.  
  126. }    /* InitializeMenus */
  127.  
  128.  
  129.     WindowPtr
  130. InitializeWindow (void)
  131. {
  132.     WindowPtr    pWindow;
  133.     Rect        bounds;
  134.     short        totalMargin;
  135.     Point        newPosition;
  136.     GDHandle    hGDevice;
  137.  
  138.     if ((pWindow = GetNewDialog (dialogRsrcID, nil, kWindowOnTop)) == nil)
  139.         return (nil);
  140.  
  141.     SetPort (pWindow);
  142.     SetDefaultViewPort (GXNewWindowViewPort (pWindow));
  143.  
  144.     /* Find the deepest screen */
  145.     SetRect (&bounds, -32767, -32767, 32767, 32767);
  146.     hGDevice = GetMaxDevice (&bounds);
  147.     bounds = (**hGDevice).gdRect;
  148.  
  149.     totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
  150.     newPosition.h = bounds.left + (totalMargin >> 1);
  151.  
  152.     totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
  153.     newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? MBarHeight : 0)) >> 1);
  154.  
  155.     MoveWindow (pWindow, newPosition.h, newPosition.v, true);
  156.     ShowWindow (pWindow);
  157.  
  158.     gDragRect = qd.screenBits.bounds;
  159.     return (pWindow);
  160.  
  161. }    /* InitializeWindow */
  162.  
  163.  
  164.     void
  165. DoContentClick (EventRecord *pEvent, WindowPtr pWindow)
  166. {
  167.     if (pWindow == nil)
  168.         return;
  169.  
  170.     if (FrontWindow () == pWindow)
  171.     {
  172.         if (pWindow == gWindow)
  173.             DoHitTestContent (pEvent, pWindow);
  174.     }
  175.     else
  176.     {
  177.         SetPort (pWindow);
  178.         SelectWindow (pWindow);
  179.     }
  180.  
  181. }    /*    DoContentClick  */
  182.  
  183.  
  184.     void
  185. DoMenuCommand (menuResult)
  186.     long        menuResult;
  187. {
  188.     short        menuID,
  189.                 itemNumber;
  190.  
  191.     if (! menuResult)
  192.         return;
  193.  
  194.     menuID       = HiWord (menuResult);
  195.     itemNumber = LoWord (menuResult);
  196.  
  197.     switch (menuID)
  198.     {
  199.         case appleMenuRsrcID:
  200.             switch (itemNumber)
  201.             {
  202.                 case itemAbout:
  203.                     Alert (aboutRsrcID, nil);
  204.                     break;
  205.  
  206.                 default:
  207. //                    DoDeskAcc (appleMenuRsrcID, itemNumber);
  208.                     SysBeep (1);
  209.                     break;
  210.             }
  211.             break;
  212.  
  213.         case fileMenuRsrcID:
  214.             switch (itemNumber)
  215.             {
  216.                 case itemQuit:
  217.                     gQuitApp = true;
  218.                     break;
  219.  
  220.                 default:
  221.                     SysBeep (1);
  222.                     break;
  223.             }
  224.             break;
  225.  
  226.         case metricsMenuRsrcID:
  227.             switch (itemNumber)
  228.             {
  229.                 case itemShowControlPoints:
  230.                 case itemShowLocalBounds:
  231.                     ToggleMetricsMenuItem (itemNumber, gWindow);
  232.                     break;
  233.  
  234.                 case itemAutoMouse:
  235.                     ToggleAutoMouse ();
  236.                     break;
  237.  
  238.                 default:
  239.                     SysBeep (1);
  240.                     break;
  241.             }
  242.             break;
  243.  
  244.         default:
  245.             break;
  246.     }  /*  switch (menuID)  */
  247.  
  248.     HiliteMenu (kHiliteAllMenus);
  249.  
  250. }    /*    DoMenuCommand  */
  251.  
  252.  
  253.     void
  254. DoGrowBox (EventRecord *pEvent, WindowPtr pWindow)
  255. {
  256.     long        newSize;
  257.     Rect        newRect,
  258.                 oldRect;
  259.  
  260.     oldRect = newRect = pWindow->portRect;
  261.  
  262.     if (newSize = GrowWindow (pWindow, pEvent->where, &qd.screenBits.bounds))
  263.     {
  264.         newRect.right    = newRect.left + (short) newSize;
  265.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  266.         SizeWindow (pWindow,
  267.                     newRect.right - newRect.left,
  268.                     newRect.bottom - newRect.top,
  269.                     true);
  270.         InvalRect (&oldRect);
  271.     }
  272.  
  273. }    /*    DoGrowBox  */
  274.  
  275.  
  276.     void
  277. DoZoomBox (EventRecord *pEvent, WindowPtr pWindow, short windowPart)
  278. {
  279.     if (TrackBox (pWindow, pEvent->where, windowPart))
  280.     {
  281.         ZoomWindow (pWindow, windowPart, true);
  282.         InvalRect (&pWindow->portRect);
  283.     }
  284.  
  285. }    /*    DoZoomBox  */
  286.  
  287.  
  288.     void
  289. DoNullEvent (EventRecord *pEvent)
  290. {
  291.     IdleHitTestWindow (gWindow, pEvent);
  292.     MoveAutoMouse ();
  293.  
  294. }    /*    DoNullEvent  */
  295.  
  296.  
  297.     void
  298. DoMouseDown (EventRecord *pEvent)
  299. {
  300.     short            windowPart;
  301.     WindowPtr        whichWindow;
  302.  
  303.     windowPart = FindWindow (pEvent->where, &whichWindow);
  304.  
  305.     switch (windowPart)
  306.     {
  307.     case inDesk:
  308.         break;
  309.  
  310.     case inMenuBar:
  311.         DoMenuCommand (MenuSelect (pEvent->where));
  312.         break;
  313.  
  314.     case inSysWindow:
  315.         SystemClick (pEvent, whichWindow);
  316.         break;
  317.  
  318.     case inContent:
  319.         DoContentClick (pEvent, whichWindow);
  320.         break;
  321.  
  322.     case inDrag:
  323.         DragWindow (whichWindow, pEvent->where, &gDragRect);
  324.         break;
  325.  
  326.     case inGrow:
  327.         DoGrowBox (pEvent, whichWindow);
  328.         break;
  329.  
  330.     case inGoAway:
  331.           if (TrackGoAway (whichWindow, pEvent->where))
  332.           {
  333.               if (whichWindow == gWindow)
  334.                 gQuitApp = true;
  335.             else
  336.                 DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
  337.           }
  338.         break;
  339.  
  340.     case inZoomIn:
  341.     case inZoomOut:
  342.         DoZoomBox (pEvent, whichWindow, windowPart);
  343.         break;
  344.  
  345.     default:
  346.         break;
  347. }    /* switch */
  348.  
  349. }    /*    DoMouseDown  */
  350.  
  351.  
  352.     void
  353. DoKeyStroke (EventRecord *pEvent)
  354. {
  355.     char            charCode;
  356.  
  357.     charCode = pEvent->message & charCodeMask;
  358.  
  359.     if (pEvent->modifiers & btnState)
  360.     {
  361.         /*--------------*/
  362.         /* Button is UP */
  363.         /*--------------*/
  364.         if (pEvent->modifiers & cmdKey)
  365.         {
  366.             /*-------------------*/
  367.             /* Command key    x100 */
  368.             /*-------------------*/
  369.             DoMenuCommand (MenuKey (charCode));
  370.         }
  371.     }
  372.  
  373. }    /*    DoKeyStroke  */
  374.  
  375.  
  376.     void
  377. DoUpdate (EventRecord *pEvent)
  378. {
  379.     GrafPtr            savedPort;
  380.     WindowPtr        pUpdateWindow;
  381.  
  382.     GetPort (&savedPort);
  383.  
  384.     pUpdateWindow = (WindowPtr) pEvent->message;
  385.  
  386.     SetPort (pUpdateWindow);
  387.     BeginUpdate (pUpdateWindow);
  388.  
  389.     EraseRgn (pUpdateWindow->visRgn);
  390.  
  391.     if (pUpdateWindow == gWindow)
  392.         UpdateHitTestWindow (gWindow);
  393.  
  394.     EndUpdate (pUpdateWindow);
  395.     SetPort (savedPort);
  396.  
  397. }    /*    DoUpdate  */
  398.  
  399.  
  400.     void
  401. DoActivate (EventRecord *pEvent)
  402. {
  403.     WindowPtr        pActiveWindow;
  404.  
  405.     /*--------------------------------------*/
  406.     /* Get the window to de/activate        */
  407.     /* and its kind from the event message    */
  408.     /*--------------------------------------*/
  409.     pActiveWindow = (WindowPtr) pEvent->message;
  410.  
  411.     SetPort (pActiveWindow);
  412.  
  413.     /*-----------------*/
  414.     /* Activate window */
  415.     /*-----------------*/
  416.     if (pEvent->modifiers & activeFlag)
  417.     {
  418.         /* Enable/Disable items */
  419.     }
  420.     else
  421.     /*-------------------*/
  422.     /* Deactivate window */
  423.     /*-------------------*/
  424.     {
  425.         /* Enable/Disable items */
  426.     }
  427.  
  428. }    /*    DoActivate  */
  429.  
  430.  
  431.     void
  432. DoEvent  (void)
  433. {
  434.     EventRecord    theEvent;
  435.  
  436.     SystemTask ();        /* Handle desk accessories */
  437.  
  438.     GetNextEvent (everyEvent, &theEvent);
  439.  
  440.     switch (theEvent.what)
  441.     {
  442.     case nullEvent:
  443.         DoNullEvent (&theEvent);
  444.         break;
  445.  
  446.     case mouseDown:
  447.         DoMouseDown (&theEvent);
  448.         break;
  449.  
  450.     case keyDown:
  451.     case autoKey:
  452.         DoKeyStroke (&theEvent);
  453.         break;
  454.  
  455.     case updateEvt:
  456.         DoUpdate (&theEvent);
  457.         break;
  458.  
  459.     case activateEvt:
  460.         DoActivate (&theEvent);
  461.         break;
  462.  
  463.     case mouseUp:
  464.     case keyUp:
  465.     case diskEvt:
  466.     case networkEvt:
  467.     case driverEvt:
  468.     default:
  469.         break;
  470.     }  /* switch (theEvent.what)  */
  471.  
  472. }    /* DoEvent */
  473.  
  474.  
  475. main (void)
  476. {
  477.     gQuitApp = false;
  478.     if (InitApp ())
  479.     {
  480.         InitializeMenus ();
  481.         if (gWindow = InitializeWindow ())
  482.         {
  483.             if (InitializeHitTesting (gWindow))
  484.             {
  485.                 InitializeAutoMouse (globals.boxes);
  486.  
  487.                 while (! gQuitApp)
  488.                     DoEvent ();
  489.  
  490.                 CleanupHitTesting ();
  491.             }
  492.         }
  493.         ExitApp ();
  494.     }
  495.  
  496. }    /* main */
  497.